# coding=utf-8
# Date: 15/1/29'
# Email: wangjian2254@icloud.com
import json
from django.db import models



__author__ = u'王健'

BASE_DATE_FROMATE = '%Y-%m-%d'
BASE_DATETIME_FROMATE = '%Y-%m-%d %H:%M:%S'
BASE_TIME_FORMATE = '%H:%M'


class JSONBaseMixin(object):
    """
    model json序列化掺合类
    by: 范俊伟 at:2015-06-11
    """

    def toJSON(self):
        """
        序列化成 dict类型
        :return:
        """

        fields = []
        for field in self._meta.fields:
            fields.append((field.name, field.attname, type(field)))

        d = {}
        for attr, attname, t in fields:
            if getattr(self, attname) == None:
                if t == models.DateTimeField:
                    d[attr] = ''
                elif t == models.DateField:
                    d[attr] = ''
                elif t == models.TimeField:
                    d[attr] = ''
                elif t == models.CommaSeparatedIntegerField:
                    d[attr] = []
                elif t == models.CharField:
                    d[attr] = ''
                elif t == models.IntegerField:
                    d[attr] = 0
                elif t == models.AutoField:
                    d[attr] = 0
                elif t == models.OneToOneField:
                    d[attr] = 0
                elif t == models.ForeignKey:
                    d[attr] = 0
                elif t == models.EmailField:
                    d[attr] = ""
                elif t == models.URLField:
                    d[attr] = ""
                elif t == models.TextField:
                    d[attr] = ""
                elif t == models.BigIntegerField:
                    d[attr] = 0
                elif t == models.BooleanField:
                    d[attr] = False
                elif t == models.DecimalField:
                    d[attr] = 0
                elif t == models.FloatField:
                    d[attr] = 0.0
                elif t == models.NullBooleanField:
                    d[attr] = False
                else:
                    d[attr] = ""
            else:
                if t == models.DateTimeField and not isinstance(getattr(self, attname), (str, unicode)):

                    try:
                        d[attr] = getattr(self, attname).strftime(BASE_DATETIME_FROMATE)
                    except:
                        d[attr] = '2015-06-01 01:01:01'
                elif t == models.DateField and not isinstance(getattr(self, attname), (str, unicode)):
                    try:
                        d[attr] = getattr(self, attname).strftime(BASE_DATE_FROMATE)
                    except:
                        d[attr] = '2015-06-01'
                elif t == models.TimeField and not isinstance(getattr(self, attname), (str, unicode)):
                    try:
                        d[attr] = getattr(self, attname).strftime(BASE_TIME_FORMATE)
                    except:
                        d[attr] = '00:00'
                elif t == models.CommaSeparatedIntegerField:
                    if isinstance(getattr(self, attname), (str, unicode)):
                        d[attr] = json.loads(getattr(self, attname, '[]'))
                    else:
                        d[attr] = getattr(self, attname)
                else:
                    d[attr] = getattr(self, attname)
        d['s_id'] = d.get('id')
        return d


